using System;
using Mbs.Container.Activation;
using Mbs.Container.Context;
using Mbs.Container.Lifestyle;
using Mbs.Container.Metadata;

namespace Mbs.Container
{
    public static partial class KernelExtensions
    {
   

        public static IKernel Register(this IKernel kernel, Type service, Type implementation)
        {
            if (implementation != null)
                return kernel.Register(new ComponentInfo(service, implementation));
            return kernel;
        }

        public static IKernel Register<TService, TImplementation>(this IKernel kernel, string id, string lifestyle, bool isLazyInit) where TImplementation : TService
        {
            return kernel.Register(new ComponentInfo(id, typeof(TService), typeof(TImplementation), ActivatorType.Default, lifestyle, isLazyInit));
        }

        public static IKernel Register<TService>(this IKernel kernerl, Func<ICreateContext, object> creator)
        {
            return kernerl.Register(new ComponentInfo(null, typeof(TService), creator));
        }

        public static IKernel Register<TService>(this IKernel kernerl, Func<ICreateContext, object> creator, bool isLazyInit)
        {
            return kernerl.Register(new ComponentInfo(null, typeof(TService), creator, LifestyleType.Singleton, isLazyInit));
        }


        public static IKernel Register<TService>(this IKernel kernerl,string id, Func<ICreateContext, object> creator)
        {
            return kernerl.Register(new ComponentInfo(id, typeof(TService), creator));
        }

        public static IKernel Register<TService>(this IKernel kernerl, string id, Func<ICreateContext, object> creator, bool isLazyInit)
        {
            return kernerl.Register(new ComponentInfo(id, typeof(TService), creator, LifestyleType.Singleton, isLazyInit));
        }

        public static IKernel Register<TService, TImplementation>(this IKernel kernel) where TImplementation : TService
        {
            var type = typeof(TImplementation);
            return kernel.Register(new ComponentInfo(type.FullName, typeof(TService), type));
        }

        public static IKernel Register<TService, TImplementation>(this IKernel kernel, string id) where TImplementation : TService
        {
            return kernel.Register(new ComponentInfo(id, typeof(TService), typeof(TImplementation)));
        }

        public static IKernel Register<TService, TImplementation>(this IKernel kernel, bool isLazyInit) where TImplementation : TService
        {
            var type = typeof(TImplementation);
            return kernel.Register(new ComponentInfo(type.FullName, typeof(TService), type, ActivatorType.Default, LifestyleType.Singleton, isLazyInit));
        }

        public static IKernel Register(this IKernel kernel, Type implementation)
        {
            if (implementation != null)
                return kernel.Register(new ComponentInfo(implementation.FullName, implementation, implementation));
            return kernel;
        }

        public static IKernel Register(this IKernel kernel, string id, Type implementation)
        {
            if (implementation != null)
                return kernel.Register(new ComponentInfo(id, implementation, implementation));
            return kernel;
        }

        public static IKernel Register(this IKernel kernel, string id, Type service, Type implementation)
        {
            if (implementation != null)
                return kernel.Register(new ComponentInfo(id, service, implementation));
            return kernel;
        }
    }
}
